package site.timers.chat.util.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

/**
 * @Classname GroupChatServerChannelInitializer
 * @Description TODO
 * @Version 1.0.0
 * @Date 2025/8/12 14:47
 * @Author twl
 */
public class GroupChatServerChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

//        //解码器
//        pipeline.addLast("decoder", new StringDecoder());
//        //编码器
//        pipeline.addLast("encoder", new StringEncoder());
//        //加入自己业务handler
//        pipeline.addLast("serverHandler", new GroupChatServerHandler());

        // HTTP编解码器
        pipeline.addLast(new HttpServerCodec());
        // HTTP消息聚合器
        pipeline.addLast(new HttpObjectAggregator(65536));
        // WebSocket协议处理器
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        // 自定义WebSocket处理器
        pipeline.addLast(new GroupChatServerWebSocketHandler());
    }
}
